/*
 * This the most basic demo of how to use avOS.
 *
 * Features demonstrated:
 * 	- Task creation
 * 	- Task switching
 * 	- Task sleep for given time period
 *
 */

#include <stdio.h>
#include <string.h>

#include <avos/pt.h>

#include "hw.h"

/* You should write you protothreads like this: */
void task1(pt_t *pt)
{
	/* Include pt_start() in every protothread at the very beginning */
	pt_start(pt);

	while (1) {
		/* pt_sleep() can be used to delay a protothread for a while */
		pt_sleep(pt, 1);
		printf("hello\n");
	}

	/* Finally, write pt_end() at the end of your protothread code */
	pt_end(pt);
}

/* This is another task, very similar to task1 */
void task2(pt_t *pt)
{
	pt_start(pt);

	while (1) {
		pt_sleep(pt, 3);
		printf("world\n");
	}

	pt_end(pt);
}

int main(int argc, char *argv[])
{
	pt_t pt1, pt2;

	printf("DEMO: Task creation, switching, sleep\n");
	printf("Starting avOS.\n");

	/* Initialize protothreads */
	pt_init(&pt1);
	pt_init(&pt2);

	/* Start periodic timer */
	hw_init_timer();

	/* Schedule tasks one by one */
	while (1) {
		(void) pt_schedule(&pt1, task1(&pt1));
		(void) pt_schedule(&pt2, task2(&pt2));
	}
	return 0;
}
